package com.chinairi.mall.service.impl;

import com.chinairi.common.annotation.DoPage;
import com.chinairi.mall.controller.viewobject.HomeGroundClassVO;
import com.chinairi.mall.model.HomeGroundClass;
import com.chinairi.mall.dao.HomeGroundClassMapper;
import com.chinairi.mall.service.IHomeGroundClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2020-10-14
 */
@Service
public class HomeGroundClassServiceImpl extends ServiceImpl<HomeGroundClassMapper, HomeGroundClass> implements IHomeGroundClassService {

    @Autowired
    private HomeGroundClassMapper homeGroundClassMapper;

    /**
     * 获取商品分类
     *
     * @return
     */
    @Override
    @DoPage
    public List<HomeGroundClassVO> getALLClass() {
        List<HomeGroundClassVO> homeGroundClassVOList = homeGroundClassMapper.getAllClass();
        return homeGroundClassVOList.stream().filter(vo -> vo.getHomeGroundClassLevel() == 1)
                .peek(vo -> vo.setHomeGroundClassVOList(getChildList(vo,homeGroundClassVOList)))
                .collect(Collectors.toList());
    }

    /**
     * 分级获取子类
     * @param homeGroundClassVO
     * @param list
     * @return
     */
    private List<HomeGroundClassVO> getChildList(HomeGroundClassVO homeGroundClassVO, List<HomeGroundClassVO> list) {
        return list.stream().filter(vo -> vo.getHomeGroundClassParentDbid().equals(homeGroundClassVO.getHomeGroundClassDbid()))
                .peek(vo -> vo.setHomeGroundClassVOList(getChildList(vo, list)))
                .collect(Collectors.toList());
    }
}
